package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.config.BaseContext;
import com.itheima.po.R;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebFilter(filterName="logServiceFilter" , urlPatterns = "/*")
public class LogServiceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    public static final String BACKEND = "/backend";
    public static final String FRONT = "/front";
    public static final String EMPLOYEE_LOGIN = "/employee/login";
    public static final String EMPLOYEE_LOGOUT = "/employee/logout";
    public static final String USER_SEND_MSG = "/user/sendMsg";
    public static final String USER_LOGIN = "/user/login";
    public static final String NOT_LOGIN = "NOTLOGIN";
    public static final String DOC_HTML = "/doc.html";
//    public static final String WEBJARS = "/webjars/**"; 有bug因为字符串的startwith是判断前缀而不支持正则
    public static final String WEBJARS = "/webjars";
    public static final String SWAGGER_RESOURCE = "/swagger-resources";
    public static final String API_DOCS = "/v2/api-docs";
    public static final String FAVICON = "/favicon.ico";

    public static void main(String[] args) {
        System.out.println("/webjars/js/chunk-735c675c.be4e3cfe.js".startsWith(WEBJARS));
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;
        String requestUri = httpServletRequest.getRequestURI();
        System.out.println("filter thread id" + Thread.currentThread().getId());
        if (requestUri.startsWith(BACKEND)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.startsWith(SWAGGER_RESOURCE)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.startsWith(API_DOCS)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.startsWith(WEBJARS)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.equals(DOC_HTML)){
            filterChain.doFilter(servletRequest, servletResponse);
        }else if (requestUri.equals(FAVICON)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.startsWith(FRONT)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.equals(EMPLOYEE_LOGIN)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else if (requestUri.equals(EMPLOYEE_LOGOUT)){
            filterChain.doFilter(servletRequest, servletResponse);
        } else if (requestUri.equals(USER_SEND_MSG)){
            filterChain.doFilter(servletRequest, servletResponse);
        }else if (requestUri.equals(USER_LOGIN)){
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            Long employeeId = (Long)httpServletRequest.getSession().getAttribute("employeeId");
            Long userId = (Long)httpServletRequest.getSession().getAttribute("userId");
            if (null != employeeId || null != userId){

                if (null != employeeId){
                    BaseContext.setCurrentId((Long) httpServletRequest.getSession().getAttribute("employeeId"));
                }else {
                    BaseContext.setCurrentId((Long) httpServletRequest.getSession().getAttribute("userId"));
                }
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                ServletOutputStream outputStream = httpServletResponse.getOutputStream();
                outputStream.write(JSON.toJSONString(R.error(NOT_LOGIN)).getBytes(StandardCharsets.UTF_8));
            }
        }
    }

    @Override
    public void destroy() {

    }
}
